package snowflake

import (
	"github.com/bwmarrin/snowflake"
	"github.com/spf13/viper"
	"time"
)

type Sf struct {
	node *snowflake.Node
}

func NewSnowflake(conf *viper.Viper) *Sf {
	st, err := time.Parse("2006-01-02", conf.GetString("snowflake.start_time"))
	if err != nil {
		return nil
	}
	//使用 UnixNano() 方法获取其纳秒级时间戳，最后将其除以 1000000 得到毫秒级的时间戳
	snowflake.Epoch = st.UnixNano() / 1000000
	node, err := snowflake.NewNode(conf.GetInt64("snowflake.machine_id"))
	if err != nil {
		return nil
	}
	return &Sf{node: node}
}

func (s *Sf) GetID() int64 {
	return int64(s.node.Generate())
}
